package com.tjtt.common.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.*;

import com.tjtt.tdm.base.PubMethod;
import com.tjtt.tdm.flow.bean.Flowstate;
import com.tjtt.tdm.flow.bean.Flowtyperight;
import com.tjtt.tdm.flow.bean.StepEntity;


public class TotalFlowXmlTool {
    private String filePath;
    
    public TotalFlowXmlTool(String _filePath)
    {
    	try{
    		filePath = _filePath;
    	}
    	catch(Exception exp)
    	{
    		exp.printStackTrace();
    	}
    }
    
    /**
     * 获得起始节点状态名称
     * @return
     */
    public Flowstate GetFlowStateByID(String id)
    {
    	Flowstate flowstate = new Flowstate();
    	FlowXmlTool fx = new FlowXmlTool(filePath);
		Node proc0 = fx.GetXmlNode("id",id);
		flowstate.setStateid(proc0.valueOf("@id"));
		flowstate.setState(proc0.valueOf("@text"));
		String forminfo = proc0.valueOf("@form");
		flowstate.setForm(forminfo);
		if (forminfo!=null){
			String formid = forminfo.split(",")[0];
			flowstate.setFormid(formid);
		}
		flowstate.setRoleIds(proc0.valueOf("@roleid"));
		flowstate.setRoleNames(proc0.valueOf("@role"));
		flowstate.setUserIds(proc0.valueOf("@readerid"));
		flowstate.setUserNames(proc0.valueOf("@reader"));
		flowstate.setDept(proc0.valueOf("@dept"));
		flowstate.setDeptid(proc0.valueOf("@deptid"));
		String nodeType = proc0.valueOf("@actFlag");
		if (nodeType!=null&&nodeType.length()>=4){
			nodeType = nodeType.substring(0,4);
		}
		flowstate.setNodetype(nodeType);
		flowstate.setIsselectperson(proc0.valueOf("@isselectperson"));
		return flowstate;
    }
    
    /**
     * 获得起始节点状态名称
     * @return
     */
    public List<Flowstate> GetNextFlowStateByID(String id)
    {
    	List<Flowstate> lists = new ArrayList<Flowstate>();
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	List<StepEntity> steplist = fx.GetStepList("from",id);
    	for(int i=0;i<steplist.size();i++){
    		StepEntity steptentity = steplist.get(i);
    		String stateid = steptentity.getTo();
    		Flowstate flowstate = this.GetFlowStateByID(stateid);
    		lists.add(flowstate);
    	}
    	return lists;
    }
    
    public String GetForms()
    {
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	return fx.GetForms();
    }
    
    /**
     * 获得起始节点状态名称
     * @return
     */
    public Flowstate GetFlowBeginState()
    {
    	Flowstate flowstate = new Flowstate();
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	Node step0 = fx.GetXmlStep("from","begin");
		Node proc0 = fx.GetXmlNode("id",step0.valueOf("@to"));
		flowstate.setStateid(proc0.valueOf("@id"));
		flowstate.setState(proc0.valueOf("@text"));
		flowstate.setForm(proc0.valueOf("@form"));
		flowstate.setRoleIds(proc0.valueOf("@roleid"));
		flowstate.setRoleNames(proc0.valueOf("@role"));
		flowstate.setUserIds(proc0.valueOf("@readerid"));
		flowstate.setUserNames(proc0.valueOf("@reader"));
		flowstate.setDept(proc0.valueOf("@dept"));
		flowstate.setDeptid(proc0.valueOf("@deptid"));
		flowstate.setHuc(proc0.valueOf("@huc"));
		flowstate.setTongy(proc0.valueOf("@tongy"));
		flowstate.setIsjiany(proc0.valueOf("@isjiany"));
		flowstate.setIsselectperson(proc0.valueOf("@isselectperson"));
		flowstate.setNodetype(proc0.valueOf("@actFlag"));
		return flowstate;
    }
    
    /**
     * 获得起始节点的下一个状态名称
     * @return
     */
    public Flowstate GetFlowBeginToNextOneState()
    {
    	Flowstate flowstate = new Flowstate();
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	Node step0 = fx.GetXmlStep("from","begin");
    	Node proc0 = fx.GetXmlNode("id",step0.valueOf("@to"));
    	flowstate.setForm(proc0.valueOf("@form"));
    	Node step1 = fx.GetXmlStep("from",proc0.valueOf("@id"));
    	Node proc1 = fx.GetXmlNode("id",step1.valueOf("@to"));
    	flowstate.setStateid(proc1.valueOf("@id"));
    	flowstate.setState(proc1.valueOf("@text"));
    	return flowstate;
    }
    
    public String GetActionTypeByStepID(String stepID)
    {
    	Flowstate flowstate = new Flowstate();
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	Node step0 = fx.GetXmlStep("id",stepID);
    	return step0.valueOf("@actionType").toString();
    }
    
    /**
     * 获得开始节点配置的formId
     * @return
     */
    public String getStartStateID(){
    	FlowXmlTool fx = new FlowXmlTool(filePath);
		Node proc0 = fx.GetXmlNode("id","begin");
		if (proc0.valueOf("@form") !=null && !"".equals(proc0.valueOf("@form"))){
			return proc0.valueOf("@form").toString();
		}
    	return "";
    }
    
    /**
     * 根据stateId查询form，（例如：stateID是gcbz）
     * @param stateID
     * @return
     */
    public String GetFormByStateID(String stateID)
    {
    	FlowXmlTool fx = new FlowXmlTool(filePath);
		Node proc0 = fx.GetXmlNode("id",stateID);
		if (proc0.valueOf("@form") !=null && !"".equals(proc0.valueOf("@form"))){
			return proc0.valueOf("@form").toString();
		}
		return "";
    }
    
    /**
     * 根据stateId查询form，（例如：stateID是gcbz）
     * @param stateID
     * @return
     */
    public String GetReaderidByStateID(String stateID)
    {
    	FlowXmlTool fx = new FlowXmlTool(filePath);
		Node proc0 = fx.GetXmlNode("id",stateID);
		if (proc0.valueOf("@readerid") !=null && !"".equals(proc0.valueOf("@readerid"))){
			return proc0.valueOf("@readerid").toString();
		}
		return "";
    }
    
    /**
     * 根据流程状态(text)，得到id节点，查询from为id节点的值的list
     * @param attr
     * @param value
     * @return
     */
    public List<StepEntity> GetListByText(String id)
    {
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	Node proc = fx.GetXmlNode("id",id);
    	List<StepEntity> stepentityList = new ArrayList<StepEntity>();
    	String procid = proc.valueOf("@id");
		stepentityList = fx.GetStepList("from", procid);
    	return stepentityList;
    }
    
    /**
     * 根据流程状态(text)，得到id节点，查询from为id节点的值的list打回状态
     * @param attr
     * @param value
     * @return
     */
    public List<StepEntity> GetListByTextback(String id)
    {
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	Node proc = fx.GetXmlNode("id",id);
    	List<StepEntity> stepentityList = new ArrayList<StepEntity>();
    	String procid = proc.valueOf("@id");
		stepentityList = fx.GetStepListBack("from", procid);
    	return stepentityList;
    }
    
    /**
     * 根据ID获得流程分支数
     * @param attr
     * @param value
     * @return
     */
    public int GetFlowcountByID(String id)
    {
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	List<StepEntity> stepentityList = new ArrayList<StepEntity>();
    	stepentityList = fx.GetStepList("to", id);
    	return stepentityList.size();
    }
    
    /**
     * 获取下一个节点状态实例
     * @param stepid
     * @return
     */
    public Flowstate GetNextState(String stepid)
    {
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	String nextstate = "";
    	if (stepid.indexOf("_")>=0){
    		String[] parentnodes = stepid.split("_");
    		Node node = fx.GetXmlNode("id", parentnodes[0]);
    		String childfilename = node.valueOf("@childfilename");
    		String path = PubMethod.GetFlowFilePath();
	    	String filepath1 = path+childfilename;
	    	FlowXmlTool fx1 = new FlowXmlTool(filepath1);
	    	Node step = fx1.GetXmlStep("id", parentnodes[1]);
		    if (step!=null){
		    	Flowstate flowstate = new Flowstate();
		        Node node1 = fx1.GetXmlNode("id", step.valueOf("@to"));
        		flowstate.setStateid(node1.valueOf("@id"));
        		flowstate.setState(node1.valueOf("@text"));
        		flowstate.setForm(node1.valueOf("@form"));
        		flowstate.setRoleIds(node1.valueOf("@roleid"));
        		flowstate.setRoleNames(node1.valueOf("@role"));
        		flowstate.setUserIds(node1.valueOf("@readerid"));
        		flowstate.setUserNames(node1.valueOf("@reader"));
        		flowstate.setDept(node1.valueOf("@dept"));
        		flowstate.setDeptid(node1.valueOf("@deptid"));
        		flowstate.setHuc(node1.valueOf("@huc"));
        		flowstate.setTongy(node1.valueOf("@tongy"));
        		flowstate.setIsjiany(node1.valueOf("@isjiany"));
        		String nodetype = node1.valueOf("@actFlag");
		        if (nodetype!=null && nodetype.length()>=4){
		        	nodetype = nodetype.substring(0, 4);
		        }
        		flowstate.setNodetype(nodetype);
        		flowstate.setIsselectperson(node1.valueOf("@isselectperson"));
        		return flowstate;
		    }
    	}else{
    		Node step = fx.GetXmlStep("id", stepid);
    		Node node = fx.GetXmlNode("id", step.valueOf("@to"));
    	    String childfilename = node.valueOf("@childfilename");
    	    if (childfilename.equals("")){
    	    	Flowstate flowstate = new Flowstate();
    			flowstate.setStateid(node.valueOf("@id"));
    		    flowstate.setState(node.valueOf("@text"));
    		    flowstate.setForm(node.valueOf("@form"));
    		    flowstate.setRoleIds(node.valueOf("@roleid"));
        		flowstate.setRoleNames(node.valueOf("@role"));
        		flowstate.setUserIds(node.valueOf("@readerid"));
        		flowstate.setUserNames(node.valueOf("@reader"));
        		flowstate.setDept(node.valueOf("@dept"));
        		flowstate.setDeptid(node.valueOf("@deptid"));
        		flowstate.setHuc(node.valueOf("@huc"));
        		flowstate.setTongy(node.valueOf("@tongy"));
        		flowstate.setIsjiany(node.valueOf("@isjiany"));
        		String nodetype = node.valueOf("@actFlag");
		        if (nodetype!=null && nodetype.length()>=4){
		        	nodetype = nodetype.substring(0, 4);
		        }
        		flowstate.setNodetype(nodetype);
        		flowstate.setIsselectperson(node.valueOf("@isselectperson"));
    			return flowstate;
    	    }else{
    	    	String path = PubMethod.GetFlowFilePath();
    	    	String filepath1 = path+childfilename;
    	    	FlowXmlTool fx1 = new FlowXmlTool(filepath1);
    	    	Node step0 = fx1.GetXmlStep("from","begin");
    			Node proc0 = fx1.GetXmlNode("id",step0.valueOf("@to"));
    			Flowstate flowstate = new Flowstate();
    			flowstate.setStateid(proc0.valueOf("@id"));
    		    flowstate.setState(proc0.valueOf("@text"));
    		    flowstate.setForm(proc0.valueOf("@form"));
    		    flowstate.setRoleIds(proc0.valueOf("@roleid"));
        		flowstate.setRoleNames(proc0.valueOf("@role"));
        		flowstate.setUserIds(proc0.valueOf("@readerid"));
        		flowstate.setUserNames(proc0.valueOf("@reader"));
        		flowstate.setDept(proc0.valueOf("@dept"));
        		flowstate.setDeptid(proc0.valueOf("@deptid"));
        		flowstate.setHuc(proc0.valueOf("@huc"));
        		flowstate.setTongy(proc0.valueOf("@tongy"));
        		flowstate.setIsjiany(proc0.valueOf("@isjiany"));
        		String nodetype = proc0.valueOf("@actFlag");
		        if (nodetype!=null && nodetype.length()>=4){
		        	nodetype = nodetype.substring(0, 4);
		        }
        		flowstate.setNodetype(nodetype);
        		flowstate.setIsselectperson(proc0.valueOf("@isselectperson"));
    			return flowstate;
    	    }
    	}
    	return null;
    }
    
    public  Flowtyperight getFlowrightbystateid(String stateID)
    {
    	FlowXmlTool fx = new FlowXmlTool(filePath);
		Node proc0 = fx.GetXmlNode("id",stateID);
		Flowtyperight entity = new Flowtyperight();
		if (proc0.valueOf("@roleid") !=null && !"".equals(proc0.valueOf("@roleid"))){
			entity.setRoleid(proc0.valueOf("@roleid").toString());
		}
		if (proc0.valueOf("@role") !=null && !"".equals(proc0.valueOf("@role"))){
			entity.setRole(proc0.valueOf("@role").toString());
		}
		if (proc0.valueOf("@readerid") !=null && !"".equals(proc0.valueOf("@readerid"))){
			entity.setReaderid(proc0.valueOf("@readerid").toString());
		}
		if (proc0.valueOf("@reader") !=null && !"".equals(proc0.valueOf("@reader"))){
			entity.setReader(proc0.valueOf("@reader").toString());
		}
		if (proc0.valueOf("@deptid") !=null && !"".equals(proc0.valueOf("@roleid"))){
			entity.setDeptid(proc0.valueOf("@deptid").toString());
		}
		if (proc0.valueOf("@dept") !=null && !"".equals(proc0.valueOf("@dept"))){
			entity.setDept(proc0.valueOf("@dept").toString());
		}
		return entity;
    }
    
    public  List<Flowstate> GetFormlists()
    {
    	List<Flowstate> lists = new ArrayList<Flowstate>();
    	FlowXmlTool fx = new FlowXmlTool(filePath);
    	List nodelist = fx.GetFormnodes();
    	for(int i=0;i<nodelist.size();i++)
    	{
    		Node node = (Node)nodelist.get(i);
    		Flowstate state = new Flowstate();
    		String forminfo = node.valueOf("@form");
    		if (forminfo!=null){
    			String formid = forminfo.split(",")[0];
    			state.setFormid(formid);
    		}
    		state.setStateid(node.valueOf("@id"));
    		state.setState(node.valueOf("@text"));
    		state.setForm(forminfo);
    		lists.add(state);
    	}
    	return lists;
    }
}
